package dbcTools;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

import xmlTools.XMLUtil;

public class DBC_MySQL {
	// 声明Connection对象
	private Connection connection;
	// 驱动程序名
	private final static String driver = "com.mysql.jdbc.Driver";
	// 数据库名称
	private String databaseName;
	// 数据库ip地址
	private String ip;
	// 数据库端口
	private String port;
	// MySQL配置时的用户名
	private String userName;
	// MySQL配置时的密码
	private String password;
	//设置useSSL防止警告
	private String useSSL;

	public DBC_MySQL(String fileRoute) {
		try {
			// 加载驱动程序
			Class.forName(driver);
			
			XMLUtil xmlUtil = new XMLUtil(fileRoute);
			databaseName = xmlUtil.getFirstNodeValue("databaseName");
			ip = xmlUtil.getFirstNodeValue("ip");
			port = xmlUtil.getFirstNodeValue("port");
			userName = xmlUtil.getFirstNodeValue("userName");
			password = xmlUtil.getFirstNodeValue("password");
			useSSL = xmlUtil.getFirstNodeValue("useSSL");
			
			
			String url = "jdbc:mysql://" + ip + ":" + port + "/" + databaseName+"?useSSL=" + useSSL;
			connection = DriverManager.getConnection(url, userName, password);
		} catch (ClassNotFoundException e) {
			// 数据库驱动类异常处理
			e.printStackTrace();
		} catch (SQLException e) {
			// 数据库连接失败异常处理
			e.printStackTrace();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public Connection getConnection(){
		return connection;
	}
}
